/*
 * @Author: szx
 * @Date: 2022-05-12 13:31:42
 * @LastEditTime: 2022-05-12 13:42:41
 * @Description:
 * @FilePath: \leetcode\面试题\01\05\05.js
 */
/**
 * @param {string} first
 * @param {string} second
 * @return {boolean}
 */
var oneEditAway = function (first, second) {
    const n = first.length,
        m = second.length;
    if (first === second) return true;
    if (Math.abs(n - m) > 1) return false;
    if (n > m) {
        return deleteOneWord(first, second);
    } else if (m > n) {
        return deleteOneWord(second, first);
    } else {
        let flag = false;
        for (let i = 0; i < n; i++) {
            if (first[i] != second[i]) {
                if (!flag) flag = true;
                else return false;
            }
        }
        return true;
    }
};

const deleteOneWord = (chang, duan) => {
    if (chang.substring(duan.length) === duan) return true;
    let j = 0;
    for (let i = 0; i < chang.length; i++) {
        if (chang[i] != duan[j]) {
            if (i != j) return false;
            continue;
        }
        j++;
    }
    return true;
};
